package sort.leetcode.easy;

/**
 * @author bruin_du
 * @description 数组相对排序
 * @date 2022/8/30 23:42
 **/
public class OfferII075_RelativeSortArray {
    // 或者使用哈希表讲数组2中的元素全部弄完后，将剩余部分放回数组进行排序
    public int[] relativeSortArray(int[] arr1, int[] arr2) {
        int[] arr = new int[1001];
        for (int i = 0; i < arr1.length; i++) {
            arr[arr1[i]]++;
        }
        int index = 0;
        for (int i = 0; i < arr2.length; i++) {
            while (arr[arr2[i]] > 0) {
                arr1[index++] = arr2[i];
                arr[arr2[i]]--;
            }
        }
        for (int i = 0; i < arr.length; i++) {
            while (arr[i] > 0) {
                arr1[index++] = i;
                arr[i]--;
            }
        }
        return arr1;
    }
}
